package cn.fl.capital.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.fl.capital.transform.CapPayListRepExportVOTransform;
import cn.fl.export.api.ICapFundExportApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPayListRepQueryCondition;
import cn.fl.capital.dto.CapPayListRepDTO;
import cn.fl.capital.service.ICapPayListRepApiService;
import cn.fl.capital.service.ICapPayListRepService;
import cn.fl.capital.transform.CapPayListRepVOTransform;
import cn.fl.capital.vo.CapPayListRepVO;
import cn.fl.framework.base.context.CurrentThreadContext;

/**
 * @Description: 资金放款报表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapPayListRepServiceImpl implements ICapPayListRepService {

	/**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(CapPayListRepServiceImpl.class);
	
	/**
	 * 资金放款报表 暴露服务
	 */
	@Autowired
	private ICapPayListRepApiService capPayListRepApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Autowired
    private ICapFundExportApiService capFundExportApiService;
	@Override
	public CapPayListRepVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapPayListRepDTO dto = this.capPayListRepApiService.selectCapPayListRepById(id);
		return CapPayListRepVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapPayListRepVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capPayListRepApiService.addCapPayListRep(CapPayListRepVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capPayListRepApiService.removeCapPayListRepByIds(ids, CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapPayListRepVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capPayListRepApiService.modifyCapPayListRep(CapPayListRepVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapPayListRepQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapPayListRepQueryCondition());
        }
		condition=(CapPayListRepQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capPayListRepApiService.searchCapPayListRepListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapPayListRepVO> voList=CapPayListRepVOTransform.toVOList((List<CapPayListRepDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapPayListRepVO> searchList(CapPayListRepQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapPayListRepQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapPayListRepVO> voList=CapPayListRepVOTransform.toVOList(this.capPayListRepApiService.searchCapPayListRepList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapPayListRepVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapPayListRepConstant.CODE_CD);
		for(CapPayListRepVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	/**
	 * 批量导出资金放款报表
	 *
	 * @param list 导出的数据集合
	 * @return
	 * @throws BaseException
	 */
	@Override
	public DataResultVO exportCapPayListRep(List<CapPayListRepVO> list) throws BaseException {
		List<cn.fl.export.dto.CapPayListRepDTO> capPayListRepVOs = null ;
		if(null == list){
			capPayListRepVOs = new ArrayList<cn.fl.export.dto.CapPayListRepDTO>();
		}else{
			capPayListRepVOs = CapPayListRepExportVOTransform.toDTOList(list);
		}
		try {
			DataResultDTO resultDTO=this.capFundExportApiService.exportCapPayListRep(capPayListRepVOs);
			if(null==resultDTO){
				return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出失败");
			}else{
				return new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo(),resultDTO.getData());
			}
		} catch (BaseException e) {
			BaseLogUtils.error(logger, "exportDecomposeList", "批量导出导出资金放款数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
		} catch (NumberFormatException e) {
			BaseLogUtils.error(logger,"exportDecomposeList","批量导出每日还款数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
		}
		return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出导出资金放款失败");
	}

	@Override
	public List<CapPayListRepVO> selectByIds(List<Long> list) throws BaseException {
		List<CapPayListRepDTO> capPayListRepDTOList = this.capPayListRepApiService.selectByIds(list);
		if(capPayListRepDTOList != null && capPayListRepDTOList.size() > 0){
			return CapPayListRepVOTransform.toVOList(capPayListRepDTOList);
		}else{
			return null;
		}
	}
}
